﻿using DeviceMonitor.Model;
using DeviceMonitor.Utils;
using LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DeviceMonitor.Windows
{
    /// <summary>
    /// ChartWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChartWindow : Window
    {
		SynchronizationContext _syncContext = null;

		public SeriesCollection seriesCollection1 { get; set; }
        public List<string> Labels1 { get; set; }
        public SeriesCollection seriesCollection2 { get; set; }
        public List<string> Labels2 { get; set; }
        public SeriesCollection seriesCollection3 { get; set; }
        public List<string> Labels3 { get; set; }

		String billingId = "";

        public ChartWindow()
        {
            InitializeComponent();
			_syncContext = SynchronizationContext.Current;

			//实例化一条折线图
			LineSeries line1 = new LineSeries();
			//设置折线的标题
			line1.Title = "流量";
			//设置折线的形式
			line1.LineSmoothness = 1;
			//折线图的无点样式
			line1.PointGeometry = null;
			//添加横坐标
			Labels1 = new List<string>();

			//添加绘图的数据
			line1.Values = new ChartValues<double>();
			seriesCollection1 = new SeriesCollection();
			seriesCollection1.Add(line1);


			LineSeries line2 = new LineSeries();
			line2.Title = "温度";
			line2.LineSmoothness = 1;
			line2.PointGeometry = null;
			Labels2 = new List<string>();
			//添加绘图的数据
			line2.Values = new ChartValues<double>();
			seriesCollection2 = new SeriesCollection();
			seriesCollection2.Add(line2);

			LineSeries line3 = new LineSeries();
			line3.Title = "密度";
			line3.LineSmoothness = 1;
			line3.PointGeometry = null;
			Labels3 = new List<string>();
			//添加绘图的数据
			line3.Values = new ChartValues<double>();
			seriesCollection3 = new SeriesCollection();
			seriesCollection3.Add(line3);


			DataContext = this;
			this.sl.Series = seriesCollection1;
			slx.Labels = Labels1;

			this.s2.Series = seriesCollection2;
			s2x.Labels = Labels2;

			this.s3.Series = seriesCollection3;
			s3x.Labels = Labels3;

			this.Loaded += Win_load;
		}

        private void Win_load(object sender, RoutedEventArgs e)
        {
			loadData();
		}

        public void setId(String id)
        {
			this.billingId = id;
        }
		private void loadData()
        {
			new Task(() =>
			{
				try
				{
					JObject paramJSON = new JObject();
					paramJSON.Add("billingId", billingId);
					paramJSON.Add("radom", Guid.NewGuid());
					
					String response = HttpPostHelper.doHttpPost("/loadLog/queryLoadLogList", paramJSON.ToString());
					JObject rspJson = JObject.Parse(response);
					bool success = (bool)rspJson["success"];
					if (success)
					{
						var jsonStr = rspJson["data"].ToString();
						List<LogModel> items = JsonConvert.DeserializeObject<List<LogModel>>(jsonStr);

						_syncContext.Post(SetDataList, items);
					}
				}
				catch (Exception ex) { }
			}).Start();
		}
		private void SetDataList(Object obj)
		{
			
			if (null == obj) return;
			List<LogModel> items = (List<LogModel>)obj;
			foreach (LogModel log in items)
			{
				Labels1.Add(log.time);
				seriesCollection1[0].Values.Add(log.flow);

				Labels2.Add(log.time);
				seriesCollection2[0].Values.Add(log.temp);

				Labels3.Add(log.time);
				seriesCollection3[0].Values.Add(log.density);
			}
		}
	}
}
